#include "bsp_time.h"
//static void MX_PWM_Init(uint16_t period);
//static void MX_PUMP_Init(void);

void MX_ATIM_Init(uint16_t period)
{
	 	  
		TMR_BaseConfig_T TMR_BaseConfigStruct;

    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR6);

    TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_BaseConfigStruct.division = SystemCoreClock/1000000-1; //1us;
    TMR_BaseConfigStruct.period = 249;
    TMR_BaseConfigStruct.repetitionCounter = 0;
    TMR_ConfigTimeBase(TMR6, &TMR_BaseConfigStruct);

    TMR_EnableInterrupt(TMR6, TMR_INT_UPDATE);
    NVIC_EnableIRQRequest(TMR6_IRQn, 3, 0);

		TMR_Enable(TMR6);
		
/************模拟串口定时器*********************************/		
//		RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);

//    TMR_BaseConfigStruct.clockDivision 			= TMR_CLOCK_DIV_1;
//    TMR_BaseConfigStruct.countMode 					= TMR_COUNTER_MODE_UP;
//    TMR_BaseConfigStruct.division 					= SystemCoreClock/3/1000000-1; //3us;
//    TMR_BaseConfigStruct.period 						= 149;//TimeSet[TimeRecvStartStep1]; //9600		    {149,  29, 29, 251,  311  },   //9600  //采集
//    TMR_BaseConfigStruct.repetitionCounter 	= 0;
//    TMR_ConfigTimeBase(TMR1, &TMR_BaseConfigStruct);

//    TMR_EnableInterrupt(TMR1, TMR_INT_UPDATE);
//    NVIC_EnableIRQRequest(TMR1_UP_IRQn, 2, 0);

//		TMR_Enable(TMR1);
		
		
/************模拟串口定时器*********************************/		
//		RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR2);

//    TMR_BaseConfigStruct.clockDivision 			= TMR_CLOCK_DIV_1;
//    TMR_BaseConfigStruct.countMode 					= TMR_COUNTER_MODE_UP;
//    TMR_BaseConfigStruct.division 					= SystemCoreClock/3/1000000-1; //3us;
//    TMR_BaseConfigStruct.period 						= 149;//TimeSet[TimeRecvStartStep1];  //9600     {149,  29, 29, 251,  311  },   //9600  //采集
//    TMR_BaseConfigStruct.repetitionCounter 	= 0;
//    TMR_ConfigTimeBase(TMR2, &TMR_BaseConfigStruct);

//    TMR_EnableInterrupt(TMR2, TMR_INT_UPDATE);
//    NVIC_EnableIRQRequest(TMR2_IRQn, 1, 0);

//		TMR_Enable(TMR2);		

/*******************PWM部分******************/

//	  MX_PWM_Init(period); //蜂鸣器
//		MX_PUMP_Init();  //泵
		
}
#if 0
void MX_PUMP_Init(void)
{
		GPIO_Config_T 		 GPIO_ConfigStruct;
    TMR_BaseConfig_T 	 TMR_TimeBaseStruct;
    TMR_OCConfig_T 		 OCcongigStruct;
    
		RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_TMR1);
		GPIO_ConfigPinRemap(GPIO_NO_REMAP_USART1);  							//完全重映射 //COCK重映射
    GPIO_ConfigStruct.pin = HeatWater_PUMP_CTR_Pin;
    GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(HeatWater_PUMP_CTR_GPIO_Port, &GPIO_ConfigStruct);
    
    GPIO_ConfigStruct.pin = NorWater_PUMP_CTR_Pin;
    GPIO_Config(NorWater_PUMP_CTR_GPIO_Port, &GPIO_ConfigStruct);
    
	
    TMR_TimeBaseStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_TimeBaseStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_TimeBaseStruct.division = SystemCoreClock/1000000*5-1; //5us;
    TMR_TimeBaseStruct.period = 99;                     //100*5=500us 2k    
    TMR_ConfigTimeBase(TMR1, &TMR_TimeBaseStruct);
    
    OCcongigStruct.idleState = TMR_OC_IDLE_STATE_RESET;
    OCcongigStruct.mode = TMR_OC_MODE_PWM1;
    OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET;
    OCcongigStruct.nPolarity = TMR_OC_NPOLARITY_HIGH;
    OCcongigStruct.outputNState = TMR_OC_NSTATE_ENABLE;
    OCcongigStruct.outputState = TMR_OC_STATE_ENABLE;
    OCcongigStruct.polarity = TMR_OC_POLARITY_HIGH;
    OCcongigStruct.pulse = 0;
		
    TMR_ConfigOC3(TMR1, &OCcongigStruct);
    TMR_ConfigOC4(TMR1, &OCcongigStruct);
		
		TMR_ConfigOC3Preload(TMR1, TMR_OC_PRELOAD_ENABLE);
    TMR_ConfigOC4Preload(TMR1, TMR_OC_PRELOAD_ENABLE);
		
    TMR_EnableAUTOReload(TMR1);
    TMR_Enable(TMR1);
    TMR_EnablePWMOutputs(TMR1);

}
void MX_PWM_Init(uint16_t period)   //蜂鸣器
{
		GPIO_Config_T GPIO_ConfigStruct;
    TMR_OCConfig_T OCcongigStruct;
    
    RCM_EnableAPB1PeriphClock(RCM_APB1_PERIPH_TMR4);

    GPIO_ConfigStruct.pin = BUZZER_PWM_Pin;
    GPIO_ConfigStruct.mode = GPIO_MODE_AF_PP;
    GPIO_ConfigStruct.speed = GPIO_SPEED_50MHz;
    GPIO_Config(BUZZER_PWM_GPIO_Port, &GPIO_ConfigStruct);

      /* TIMER3 configuration */
    OnBuzzerPWMReset(period);
    
    OCcongigStruct.idleState = TMR_OC_IDLE_STATE_RESET;
    OCcongigStruct.mode = TMR_OC_MODE_PWM1;
    OCcongigStruct.nIdleState = TMR_OC_NIDLE_STATE_RESET;
    OCcongigStruct.nPolarity = TMR_OC_NPOLARITY_HIGH;
    OCcongigStruct.outputNState = TMR_OC_NSTATE_ENABLE;
    OCcongigStruct.outputState = TMR_OC_STATE_ENABLE;
    OCcongigStruct.polarity = TMR_OC_POLARITY_HIGH;
    OCcongigStruct.pulse = 59;
		
    TMR_ConfigOC3(TMR4, &OCcongigStruct);
 
		TMR_ConfigOC3Preload(TMR4, TMR_OC_PRELOAD_ENABLE);

    TMR_EnableAUTOReload(TMR4);
    TMR_Enable(TMR4);
    TMR_EnablePWMOutputs(TMR4);
			
}

void OnBuzzerPWMReset(uint16_t fre)  //Hz
{
//	  timer_parameter_struct timer_initpara;
//	   /* TIMER4 configuration  蜂鸣器*/ 
//		timer_initpara.prescaler         = 1000000/fre-1; //1us
//		timer_initpara.alignedmode       = TIMER_COUNTER_EDGE;
//		timer_initpara.counterdirection  = TIMER_COUNTER_UP;
//		timer_initpara.period            = 119;                         
//		timer_initpara.clockdivision     = TIMER_CKDIV_DIV1;
//		timer_initpara.repetitioncounter = 0;
//		timer_init(TIMER4,&timer_initpara);
	
	  TMR_BaseConfig_T TMR_BaseConfigStruct;
		TMR_BaseConfigStruct.clockDivision = TMR_CLOCK_DIV_1;
    TMR_BaseConfigStruct.countMode = TMR_COUNTER_MODE_UP;
    TMR_BaseConfigStruct.division = 1000000/fre-1; //1us;
    TMR_BaseConfigStruct.period = 119;

    TMR_BaseConfigStruct.repetitionCounter = 0;
    TMR_ConfigTimeBase(TMR4, &TMR_BaseConfigStruct);
}
#endif

